package com.lemon.core.common.web.notify.dingding;

import com.lemon.core.common.lang.util.SystemEnvUtils;
import com.lemon.core.common.web.http.HttpClient;
import com.lemon.core.common.web.notify.dingding.bean.DingNotifyData;
import com.lemon.core.common.web.notify.dingding.constants.BaseDingRobotEnum;
import com.lemon.core.common.web.notify.dingding.constants.LemonDingRobotEnum;

/**
 * @author lemon
 * @ClassName DingDingUtils
 * @desc 钉钉发消息工具类
 * @Date 2018/11/2 15:45
 * @Version 1.0
 **/
public class DingDingUtils {

    /**
     * 发送钉钉消息
     * @param robotEnum robotEnum
     * @param notifyData notifyData
     */
    public static void sendDingMsg(BaseDingRobotEnum robotEnum, DingNotifyData notifyData) {
        switch (notifyData.getMsgtype()) {
            case DingNotifyData.Constants.TEXT_TYPE:
                sendTextMsg(robotEnum, notifyData);
                break;
            case DingNotifyData.Constants.LINK_TYPE:
                sendLinkMsg(robotEnum, notifyData);
                break;
            case DingNotifyData.Constants.MARKDOWN_TYPE:
                sendMarkdownMsg(robotEnum, notifyData);
                break;
            case DingNotifyData.Constants.ACTIONCARD_TYPE:
                sendActionCardMsg(robotEnum, notifyData);
                break;
            default:
                break;
        }
    }

    /**
     * 发送文本消息
     * @param notifyData
     */
    private static void sendTextMsg(BaseDingRobotEnum robotEnum, DingNotifyData notifyData) {
        String webhook = BaseDingRobotEnum.PREFIX_WEBHOOK + robotEnum.getWebhook();
        String msgTemplate = String.format(DingDingMsgTemplate.TEXT_MSG, notifyData.getContent(),
                notifyData.getAtMobiles(), notifyData.getAtAll());
        HttpClient.getInstance().doJsonPost(webhook, msgTemplate);
    }

    /**
     * 发送链接消息
     * @param notifyData
     */
    private static void sendLinkMsg(BaseDingRobotEnum robotEnum, DingNotifyData notifyData) {
        String webhook = BaseDingRobotEnum.PREFIX_WEBHOOK + robotEnum.getWebhook();
        String msgTemplate = String.format(DingDingMsgTemplate.LINK_MSG, notifyData.getContent(), notifyData.getTitle(),
                notifyData.getPicUrl(), notifyData.getMessageUrl());
        HttpClient.getInstance().doJsonPost(webhook, msgTemplate);
    }

    /**
     * 发送markdown消息
     * @param notifyData
     */
    private static void sendMarkdownMsg(BaseDingRobotEnum robotEnum, DingNotifyData notifyData) {
        String webhook = BaseDingRobotEnum.PREFIX_WEBHOOK + robotEnum.getWebhook();
        String msgTemplate = String.format(DingDingMsgTemplate.MARKDOWN_MSG, notifyData.getTitle(), notifyData.getContent(),
                notifyData.getAtMobiles(), notifyData.getAtAll());
        HttpClient.getInstance().doJsonPost(webhook, msgTemplate);
    }

    private static void sendActionCardMsg(BaseDingRobotEnum robotEnum, DingNotifyData notifyData) {
        String webhook = BaseDingRobotEnum.PREFIX_WEBHOOK + robotEnum.getWebhook();
        String msgTemplate = String.format(DingDingMsgTemplate.ACTIONCARD_MSG, robotEnum.getRobotName(), notifyData.getContent(),
                notifyData.getHideAvatar(), notifyData.getBtnOrientation(), robotEnum.getReceiveGroup(),
                notifyData.getSingleURL(), notifyData.getAtMobiles(), notifyData.getAtAll());
        HttpClient.getInstance().doJsonPost(webhook, msgTemplate);
    }
}
